package com.lc.recovery.ui.activity.recoverymine;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.RecoveryOrderShowListApi;
import com.lc.recovery.http.response.RecoveryOrderShowListBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class WarehousingDetailsActivity extends AppActivity {
    @BindView(R.id.rv_keruku)
    RecyclerView rvKeruku;
    @BindView(R.id.layout_refresh)
    TwinklingRefreshLayout layoutRefresh;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.ll_bg)
    LinearLayout llBg;
    private List<RecoveryOrderShowListBean.DataBean> dataBeanList = new ArrayList<>();
    private BaseQuickAdapter<RecoveryOrderShowListBean.DataBean, BaseViewHolder> warehousingDetailsadapter;
    private String shop_id;
    private int page;
    private int total;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_warehousingdetails;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        shop_id = getIntent().getStringExtra("id");


        warehousingDetailsadapter = new BaseQuickAdapter<RecoveryOrderShowListBean.DataBean, BaseViewHolder>(R.layout.item_warehousingdetails, dataBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, RecoveryOrderShowListBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_type, bean.getShops());
                baseViewHolder.setText(R.id.tv_date, bean.getCreate_time());
                baseViewHolder.setText(R.id.tv_weight, bean.getWeights());
                baseViewHolder.setText(R.id.tv_price, "实付款：￥" + bean.getAmount());
            }
        };
        rvKeruku.setAdapter(warehousingDetailsadapter);
        orderShow(1);
        layoutRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                orderShow(page);

                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {


                page++;
                if (page < total) {
                    orderShowMore(page);
                } else {
                    refreshLayout.setEnableLoadmore(false);

                }


//         }
            }
        });

    }

    private void orderShow(int page) {
        EasyHttp.post(this)
                .api(new RecoveryOrderShowListApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setPage(page + "").setShops_id(shop_id)).request(new HttpCallback<RecoveryOrderShowListBean>(this) {

            @Override
            public void onSucceed(RecoveryOrderShowListBean data) {
                if (data.getCode() == 200) {
                    dataBeanList.clear();
                    dataBeanList.addAll(data.getData());
                    total = data.getCounts();
                    warehousingDetailsadapter.setNewData(dataBeanList);
                    warehousingDetailsadapter.notifyDataSetChanged();
                    if (data.getStatus()==1){
                        tvStatus.setText("待入库");
                        llBg.setBackgroundResource(R.color.colorDC8700);
                    }else {
                        llBg.setBackgroundResource(R.color.color01B467);
                        tvStatus.setText("已入库");
                    }

                } else {
                    toast(data.getMsg());
                }
            }


            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }
    private void orderShowMore(int page) {
        EasyHttp.post(this)
                .api(new RecoveryOrderShowListApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setPage(page + "").setShops_id(shop_id)).request(new HttpCallback<RecoveryOrderShowListBean>(this) {

            @Override
            public void onSucceed(RecoveryOrderShowListBean data) {
                if (data.getCode() == 200) {

                    dataBeanList.addAll(data.getData());
                    total = data.getCounts();
                    warehousingDetailsadapter.setNewData(dataBeanList);
                    warehousingDetailsadapter.notifyDataSetChanged();
                    if (data.getStatus()==1){
                        tvStatus.setText("待入库");
                        llBg.setBackgroundResource(R.color.colorDC8700);
                    }else {
                        llBg.setBackgroundResource(R.color.color01B467);
                        tvStatus.setText("已入库");
                    }

                } else {
                    toast(data.getMsg());
                }
            }


            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }
}
